<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Withdraw\RefreshWithdrawBalanceGrid;
use App\Admin\Repositories\UserWallet;
use App\Services\Base\GoogleCaptchaService;
use App\Services\BlockChainApiServices;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserWalletController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserWallet(), function (Grid $grid) {

            $grid->column('id')->sortable();
//            $grid->column('uid');
            $grid->column('address');
            $grid->column('key', "密钥")->display(function ($v) {
                return $v ? '*******-******-******' : "";
            });
            $grid->column('system_value', "系统币余额")->action(RefreshWithdrawBalanceGrid::class);
            $grid->column('value')->action(RefreshWithdrawBalanceGrid::class);

//            $grid->column('system_value')->numberFormat();
//            $grid->column('value')->numberFormat();
//            $grid->column('status')->switch();
            $grid->column('sort');
//            $grid->column('created_at');
            $grid->column('updated_at', "最后修改")->sortable();

            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
                $filter->equal('address')->width(3);

            });

            // 禁用新增
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserWallet(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('address');
            $show->field('key');
            $show->field('system_value');
            $show->field('value');
            $show->field('status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserWallet(), function (Form $form) {
            $id = $form->getKey();
            $form->display('id');
//            $form->text('uid');
            $form->text('address');
//            $form->text('system_value');
//            $form->text('value');
            if ($id) {
                $form->text('key')->help("密钥输入会自动加密，如果不变则填空")->customFormat(function () {
                    return '';
                });

            } else {
                $form->text('key')->help("密钥输入会自动加密，如果不变则填空");
            }

            $form->switch('status');
            $form->number('sort');

//            $form->text('opt_code', '谷歌验证码')->required();
            $form->saving(function (Form $form) {
//                $ga = new GoogleCaptchaService();
//                if (!$ga->verifyCode(Admin::user()->otp_key, $form->opt_code)) {
//                    return $form->response()->error("谷歌验证码错误");
//                }
                if ($form->key) {
                    $service = new BlockChainApiServices("tron");
                    $form->key = $service->encrypt($form->key);
                } else {
                    $form->deleteInput('key');
                }

//                $form->deleteInput('opt_code');
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
